/**
* @file main.cpp
*
* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <cstdint>
#include <iostream>
#include "pose_process.h"
#include "utils.h"
using namespace std;




namespace {
const char* kOpenPoseModelPath = "../model/openpose_opencv.om";
const string kImageDir = "../data/down";
const string imagePath="../data/image.png";
const string videoPath="../data/video.mp4";

}

void process_func(OpenPoseProcess OpenPose,const string imageDir)
{
    std::vector<string> filenames;
    Utils::GetAllFiles(imageDir,filenames);
    int file_num = filenames.size()/FRAME_LENGTH;
    cout<<"total files: "<<file_num<<endl;
    for(int i=0;i<file_num;i++)
    {
        OpenPose.fileProcess(imageDir,true,true); // dataset making & motion data testing
//        string filename=imageDir+"motion_data_"+to_string(postfix[i])+".txt";
//        cout<<filenames[i]<<endl;
//        OpenPose.txtProcess(filenames[i]);
        cout<<i+1<<" file processed."<<endl;
    }

}


int main(int argc,char* argv[])
{
    if((argc < 2) || (argv[1] == nullptr)){
        ERROR_LOG("Please input: ./main <mode-id> <image-dir>");
        INFO_LOG("mode info :\n 0: single image \n 1: image file\n 2: image file no-break mode \n 3: presenter server \n 4: video\n 5: motion vedio making\n 6: subfile processing ");
        INFO_LOG("Example1 ./main 0 ../data/image.png\n Example2 ./main 1 ../data/imagefile\n Example3 ./main 2 ../data/imagefile\n Example4 ./main 3 ../data/frames \n"
        " Example5 ./main 4 ../data/video.mp4\n Example6 ./main 5 ../data/motion.txt\n Example7 ./main 6 ../data/data_subfile");
        return FAILED;
    }
    OpenPoseProcess OpenPose(kOpenPoseModelPath);
    Result ret = OpenPose.Init();


    char mode=argv[1][0];
    string FilePath=string(argv[2]);
    switch(mode)
    {
        // 0. single image
        // 1. image file
        // 2. image file no-break mode
        // 3. presenter server
        // 4. pose video process | NOTE: now has bug, cannot use.
        // 5. convert motion txt file into skeleton video
        // 6. process file with subfiles in a user-defined manner, helping making dataset

        case '0':
            cout<<"single image mode"<<endl;
            OpenPose.imageProcess(FilePath);
            // test: ./main 0 ../data/single_image_test.jpeg
            break;
        case '1':
            cout<<"image file mode"<<endl;
            OpenPose.fileProcess(FilePath,true,true);
            // test: ./main 1 ../data/image_file_test
            // proccess image file and write result into txt file
            break;
        case '2':
            cout<<"image file no-break mode"<<endl;
            OpenPose.fileProcess(FilePath,false,true);
            // do not break
            // NOTE: for presenter mode, do not suggest use individually
            break;
        case '3':
            cout<<"presenter server mode"<<endl;
            OpenPose.presenterProcess(FilePath);
            // test: ./main 3 ../data/image_file_test/1
            // process a file of frames and present results on image on web
            // please cd script and use bash *.sh to open presenter server
            break;
        case '4':
            cout<<"openpose video mode"<<endl;
            OpenPose.videoProcess(FilePath);
            // have bugs now, cannot use
            break;
        case '5':
            cout<<"motion video mode"<<endl;
            OpenPose.txtProcess(FilePath);
            // visualize such motion_data.txt file into avi format movie
            // test: ./main 5 ../data/motion_data_test.txt
            break;
        case '6':
        // self design
            process_func(OpenPose,FilePath);
            // process a bunch of files, designed for making dataset
            // test: ./main 5 ../data/image_file_test
        break;
        default:
            cout<<"please enter number 0~6"<<endl;
            break;
    }

    ret = OpenPose.DeInit();

    return SUCCESS;
}

